package AI;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import Actor.Actor;
import Actor.IfaceCharAction;
import Map.Floor;
import Map.Tile;
import View.ViewManager;

public class BresenhamLineAI extends AbstractAI {
	private ViewManager manager;
	public BresenhamLineAI(List<Actor> _actor, List<Actor> _monseter,
			Floor _map,Map<List<Point>, Integer> bullets,ViewManager _manager ) {
		super(_actor, _monseter, _map,bullets);
		setManager(_manager);
	}

	@Override
	public void think() {
		for (Actor actor : actors) {
			for (Actor monster : monseters) {
				try{
					List<Point> list = findLine(actor.getX()/10, actor.getY()/10,
							monster.getX(), monster.getY());
					List<IfaceCharAction> listCommand = PointToComandConvertor
							.convert(list, monster);
					IfaceCharAction command = listCommand.get(0);
					if (command != null) {
						command.doAcion(map,manager);
					}
				}catch(Exception e){
					
				}
			}
		}

	}

	@Override
	public void thinkAfter() {

	}

	public List<Point> findLine(int x0, int y0, int x1, int y1)

	{

		List<Point> line = new ArrayList<Point>();
		int dx = Math.abs(x1 - x0);
		int dy = Math.abs(y1 - y0);
		int sx = x0 < x1 ? 1 : -1;
		int sy = y0 < y1 ? 1 : -1;
		int err = dx - dy;
		int e2;
		while (true) {
			line.add(new Point(x0, y0));
			if (x0 == x1 && y0 == y1)
				break;
			e2 = 2 * err;
			if (e2 > -dy) {
				err = err - dy;
				x0 = x0 + sx;
			}
			if (e2 < dx) {
				err = err + dx;
				y0 = y0 + sy;
			}
		}
		return line;

	}

	public ViewManager getManager() {
		return manager;
	}

	public void setManager(ViewManager manager) {
		this.manager = manager;
	}
}
